package com.sifang.bussiness.channel.type.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sifang.base.util.CodeUtils;
import com.sifang.base.util.SignUtil;
import com.sifang.bussiness.channel.context.*;
import com.sifang.bussiness.channel.type.ChannelMethod;
import com.sifang.bussiness.channel.util.HttpUtils;

import java.math.BigDecimal;
import java.util.Map;
import java.util.Random;

/**
 * 测试
 * @author 18319
 *
 */
public class TestImpl implements ChannelMethod{
	
	private static Logger log = LoggerFactory.getLogger(TestImpl.class);

	@SuppressWarnings("unused")
	@Override
	public ChannelRspPay pay(ChannelReqPay req) {
		ChannelRspPay channelRspPay = new ChannelRspPay();
		String orderNumber = CodeUtils.createUniqueCode();
		channelRspPay.setCh_code(orderNumber);
		channelRspPay.setCode_url("http://www.javatyro.pw.img.800cdn.com/075809431e0b5bcbb58b2ec131caf21.png");
		channelRspPay.setMsg("成功");
		Random random = new Random();              
        //产生随机数
        int number = random.nextInt(10 - 0 + 1) + 0;
		//int number = 0;
        int status = 0;
        if(number%3 == 0) {
        	status = 1;
        }
        
		channelRspPay.setStatus(status);
		
		//模拟实际少支付10元
		BigDecimal less = new BigDecimal("1000");
		//BigDecimal real_fee = req.getMpPay().getSub_fee().subtract(less);
		BigDecimal real_fee = req.getMpPay().getSub_fee();
		
		if(status == 0) {
			new Thread(new Runnable() {
				
				@Override
				public void run() {
					try {
						Thread.sleep(5000);
						Random random = new Random();              
				        //产生随机数
				        int number = random.nextInt(10 - 0 + 1) + 0;
						//int number = 0;
				        int status = 0;
				        BigDecimal t_real_fee = real_fee;
				        if(number%3 == 0) {
				        	log.info("模拟支付少给10块钱");
				        	//模拟少支付10块钱
				        	BigDecimal lessTenYuan = new BigDecimal("1000.00");
				        	t_real_fee = t_real_fee.subtract(lessTenYuan);
				        }
						//回调请求
						String req_url = req.getChannel_notify_url();
						log.info("模拟请求地址：{}",req_url);
						String post_data = "channel_code="+req.getMpPay().getChannel_code()+"&chCode="+orderNumber+"&cusValue=mmplo&mchCode="+req.getMpPay().getCode()+"&fee="+t_real_fee;
						String result = HttpUtils.sendPost(req_url, post_data);
						log.info("模拟 请求返回结果：{}",result);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					
				}
			}).start();
		}
		return channelRspPay;
	}
	
	@Override
	public ChannelRspPayNotify parsePayNotify(String msg, String channel_code, String channel_secret_key) {
		//报文解析
		Map<String,Object> reqMap = SignUtil.getMapFromStr(msg);
		ChannelRspPayNotify pay = new ChannelRspPayNotify();
		pay.setStatus(ChannelRspPayNotify.RSP_STATUS_NOTIFY_PAY_SUCCESS);
		
		pay.setChannel_code(reqMap.get("channel_code")+"");
		pay.setCh_code(reqMap.get("chCode")+"");
		BigDecimal fee = new BigDecimal(reqMap.get("fee")+"");
		pay.setFee(fee);
		pay.setCode(reqMap.get("mchCode")+"");
		pay.setRspMsg("success");
		return pay;
	}

	@Override
	public ChannelRspCheckPay checkPay(ChannelReqCheckPay req) {
		ChannelRspCheckPay channelRspCheckPay = new ChannelRspCheckPay();
		channelRspCheckPay.setCh_code(req.getMpPay().getCh_code());
		channelRspCheckPay.setCode(req.getMpPay().getCode());
		channelRspCheckPay.setFee(req.getMpPay().getFee());
		channelRspCheckPay.setMsg("成功");
		channelRspCheckPay.setStatus(ChannelRspCheckPay.RSP_STATUS_CHECKPAY_SUCCESS);
		return channelRspCheckPay;
	}

	@Override
	public ChannelRspWithdrawal withdrawal(ChannelReqWithdrawal req) {
		ChannelRspWithdrawal rsp = new ChannelRspWithdrawal();
		String orderNumber = CodeUtils.createUniqueCode();
		rsp.setCh_code(orderNumber);
		rsp.setMsg("成功");
		Random random = new Random();
		//产生随机数
        int number = random.nextInt(10 - 0 + 1) + 0;
		//int number = 0;
        int status = 0;
        if(number%4 == 0) {
        	status = 1;
        }
        
		rsp.setStatus(status);
		log.info("测试渠道-下单状态A：{}",status);
		//产生随机数
        //int number = random.nextInt(4 - 0 + 1) + 0;
		//模拟少到账10块钱
		//BigDecimal real_fee = req.getWithdrawal().getSub_fee().subtract(less);
		BigDecimal real_fee = req.getWithdrawal().getSub_fee();
		
		if(status == 0) {
			new Thread(new Runnable() {
				
				@SuppressWarnings("unused")
				@Override
				public void run() {
					try {
						int sleep = random.nextInt(10000 - 0 + 1000) + 0;
						Thread.sleep(sleep);
						Random random = new Random();              
				        //产生随机数
				        int number = random.nextInt(10 - 0 + 1) + 0;
						//int number = 0;
				        int status = 0;
				        //模拟少支付10块钱
			        	BigDecimal lessTenYuan = new BigDecimal("1000.00");
				        //BigDecimal t_real_fee = real_fee.subtract(lessTenYuan);
			        	BigDecimal t_real_fee = real_fee;
				        /*if(number%3 == 0) {
				        	log.info("模拟代付少给10块钱");
				        	//模拟少支付10块钱
				        	BigDecimal lessTenYuan = new BigDecimal("1000.00");
				        	t_real_fee = t_real_fee.subtract(lessTenYuan);
				        }*/
						//回调请求
						String req_url = req.getChannel_notify_url();
						String post_data = "channel_code="+req.getWithdrawal().getChannel_code()+"&chCode="+orderNumber+"&cusValue=mmplo&mchCode="+req.getWithdrawal().getCode()+"&fee="+t_real_fee;
						String result = HttpUtils.sendPost(req_url, post_data);
						log.info("模拟 请求返回结果：{}",result);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					
				}
			}).start();
		}
		return rsp;
	}
	
	@Override
	public ChannelRspWithdrawalNotify parseWithDrawalNotify(String msg, String channel_code, String channel_secret_key) {
		//报文解析
		Map<String,Object> reqMap = SignUtil.getMapFromStr(msg);
		ChannelRspWithdrawalNotify pay = new ChannelRspWithdrawalNotify();
		pay.setStatus(ChannelRspPayNotify.RSP_STATUS_NOTIFY_PAY_SUCCESS);
		
		pay.setChannel_code(reqMap.get("channel_code")+"");
		pay.setCh_code(reqMap.get("chCode")+"");
		BigDecimal fee = new BigDecimal(reqMap.get("fee")+"");
		pay.setFee(fee);
		pay.setCode(reqMap.get("mchCode")+"");
		pay.setRspMsg("success");
		return pay;
	}


	@Override
	public ChannelRspCheckWithdrawal checkWithdrawal(ChannelReqCheckWithdrawal req) {
		ChannelRspCheckWithdrawal channelRspCheckWithDrawal = new ChannelRspCheckWithdrawal();
		channelRspCheckWithDrawal.setStatus(ChannelRspCheckWithdrawal.RSP_STATUS_WITHDRAWAL_SUCCESS);
		channelRspCheckWithDrawal.setFee(req.getWithdrawal().getSub_fee());
		channelRspCheckWithDrawal.setMsg("已到账");
		channelRspCheckWithDrawal.setCh_code(req.getWithdrawal().getCh_code());
		return channelRspCheckWithDrawal;
	}

	@Override
	public ChannelRspCheckBalance checkBalance(ChannelReqCheckBalance req) {
		
		return null;
	}

}
